package Servlet;

import dao.CompileFile;
import dao.CreateFile;
import dao.Impl.CompileJavaFile;
import dao.Impl.CompilePythonFile;
import dao.Impl.CreateFileImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static java.lang.Thread.sleep;

@WebServlet("/CompileServlet")
public class CompileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        final String s= request.getParameter("value");
        final String type= request.getParameter("type");

        System.out.println(s);
        System.out.println("type=="+type);
        final PrintWriter out = response.getWriter();
        Runnable runnable;
        if(type.equals("python")){
            System.out.println("python");

            runnable=new Runnable(){
                @Override
                public void run() {
                    CreateFile createFile=new CreateFileImpl(s,"py");
                    createFile.setPythonFileName("test");
                    createFile.writeFile();
                    CompileFile compileFile=new CompilePythonFile(createFile.getFileName());
                    try {
                        String   result=compileFile.run();
                        System.out.println(result);
                        out.write(result);
                    } catch (IOException e) {
                        e.printStackTrace();
                        out.write("出现错误");
                    }
                }
            };
        }else {
            System.out.println("java");

            runnable=new Runnable(){
                @Override
                public void run() {
                    CreateFile createFile=new CreateFileImpl(s,"java");
                    createFile.setJavaFileName();
                    createFile.writeFile();
                    CompileFile compileFile=new CompileJavaFile(createFile.getFileName());
                    String result="sd";
                    String compileResult=compileFile.compile();
                    if (compileResult.equals("true")){
                        try {
                            result=compileFile.run();
                            System.out.println(result);

                            out.write(result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else{
                        out.write(compileResult);
                    };

                }
            };
        }

        Thread thread=new Thread(runnable);
        thread.start();
        try {
            sleep(1500);
            if(thread.isAlive()==true){
                thread.stop();
                out.write("运行超时");

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
